package com.neo4j.neo4jcloud.service.impl;

import com.neo4j.neo4jcloud.entity.*;
import com.neo4j.neo4jcloud.service.GraphInService;
import com.neo4j.neo4jcloud.service.SupplyRelationShipService;
import org.apache.commons.collections.IteratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author kabuqinuo
 * @date 2020/3/27 11:56
 */
@Service
public class GraphInServiceImpl implements GraphInService {

    @Autowired
    private SupplyRelationShipService supplyRelationShipService;


    @Override
    public GraphIn findGraphInByAll() {
        GraphIn graphIn = new GraphIn();
        Iterable<SupplyRelationShip> supplyGraphRepositoryIterable = supplyRelationShipService.findAllSupplyRelationShip();
        Iterator<SupplyRelationShip> iterator = supplyGraphRepositoryIterable.iterator();
        List<SupplyRelationShip> supplyRelationShipList = IteratorUtils.toList(iterator);
        if (supplyRelationShipList != null && supplyRelationShipList.size() >0){
            graphIn = getGraphIn(supplyRelationShipList);
        }
        return graphIn;
    }

    @Override
    public GraphIn findGraphInMoreByAll() {
        GraphIn graphIn = new GraphIn();
        List<SupplyRelationShip> supplyRelationShipList = supplyRelationShipService.findAllSupply();
        if (supplyRelationShipList != null && supplyRelationShipList.size() >0){
            graphIn = getGraphIn(supplyRelationShipList);
        }
        return graphIn;
    }

    @Override
    public GraphIn findGraphInMoreByNodeId(String nodeId) {
        GraphIn graphIn = new GraphIn();
        List<SupplyRelationShip> supplyRelationShipList = supplyRelationShipService.findASupplyByNodeId(nodeId);
        if (supplyRelationShipList != null && supplyRelationShipList.size() >0){
            graphIn = getGraphIn(supplyRelationShipList);
        }
        return graphIn;
    }


    private GraphIn getGraphIn(List<SupplyRelationShip> supplyRelationShipList){
        GraphIn graphIn = new GraphIn();
        List<Nodes> nodes = new ArrayList<>();
        List<Edge> edges = new ArrayList<>();
        for (SupplyRelationShip supplyRelationShip:supplyRelationShipList){
            Nodes nodes1 = new Nodes();
            Edge edge = new Edge();
            //获取每一个子节点
            nodes1.setId(supplyRelationShip.getSupplyGraph().getNodeId());
            nodes1.setShape("CircleNode");

            NodeChildren nodeChildren = new NodeChildren();
            nodeChildren.setId(supplyRelationShip.getTarget());
            nodeChildren.setLabel(supplyRelationShip.getIndexName());
            nodeChildren.setType("company");

            List<Property> properties = new ArrayList<>();
            Property property = new Property();
            property.setCode("name");
            property.setName("名称");
            property.setValue(supplyRelationShip.getSupplyGraph().getName());
            properties.add(property);
            Property property1 = new Property();
            property1.setCode("nodeId");
            property1.setName("唯一标识");
            property1.setValue(supplyRelationShip.getSupplyGraph().getNodeId());
            properties.add(property1);
            Property property2 = new Property();
            property2.setCode("type");
            property2.setName("类型");
            property2.setValue(supplyRelationShip.getSupplyGraph().getType());
            properties.add(property2);
            Property property3 = new Property();
            property3.setCode("industry");
            property3.setName("行业");
            property3.setValue(supplyRelationShip.getSupplyGraph().getIndustry());
            properties.add(property3);
            Property property4 = new Property();
            property4.setCode("phone");
            property4.setName("手机号");
            property4.setValue(supplyRelationShip.getSupplyGraph().getPhone());
            properties.add(property4);

            nodeChildren.setProperties(properties);
            nodes1.setData(nodeChildren);
            nodes.add(nodes1);

            edge.setSource(supplyRelationShip.getSource());
            edge.setTarget(supplyRelationShip.getTarget());
            edge.setLabel(supplyRelationShip.getTarget());
            EdgeData edgeData = new EdgeData();
            edgeData.setSource(supplyRelationShip.getSource());
            edgeData.setTarget(supplyRelationShip.getTarget());
            List<Property> properties1 = new ArrayList<>();
            edgeData.setProperties(properties1);
            edge.setData(edgeData);
            edges.add(edge);
        }
        graphIn.setNodes(nodes);
        graphIn.setEdges(edges);

        return graphIn;
    }
}
